package health.lm.com.mine.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.lm.sjy.titlebar.widget.CommonTitleBar;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import health.lm.com.component_base.arouter.Router;
import health.lm.com.component_base.base.event.IBus;
import health.lm.com.component_base.base.event.RxBusImpl;
import health.lm.com.component_base.base.mvp.activity.XActivity;
import health.lm.com.component_base.widget.SuperDividerItemDecoration;
import health.lm.com.mine.R;
import health.lm.com.mine.R2;
import health.lm.com.mine.adapter.GrowthIntegralAdapter;
import health.lm.com.mine.entity.GrowthIntegralEntity;
import health.lm.com.mine.present.GrowthIntegralPresenter;
import health.lm.com.router.ARouterConstant;

import static com.lm.sjy.titlebar.widget.CommonTitleBar.ACTION_LEFT_BUTTON;
import static health.lm.com.component_base.arouter.Router.WebActivity;

/**
 * @author GQI
 * @description 成长记录
 * @date :2019-08-07 13:54
 */
@Route(path = ARouterConstant.GrowthIntegralActivity, extras = ARouterConstant.NEED_LOGIN)
public class GrowthIntegralActivity extends XActivity<GrowthIntegralPresenter> {
    @BindView(R2.id.title_bar)
    CommonTitleBar titlebar;
    @Autowired
    Bundle jumpBundle;
    @BindView(R2.id.rlv_growth)
    RecyclerView rlvGrowth;
    private GrowthIntegralAdapter adapter;
    private GrowthIntegralEntity entity;
    private List<GrowthIntegralEntity.Menu> dataList;
    private TextView jfCount,jfToday;

    @Override
    public int getLayoutId() {
        return R.layout.activity_growth_integral;
    }

    @Override
    public GrowthIntegralPresenter newP() {
        return new GrowthIntegralPresenter();
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        titlebar.setListener((v, action, extra) -> {
            if (ACTION_LEFT_BUTTON == action) {
                finish();
            }
        });
        initAdapter();
        getP().getData();
    }


    /**
     * 初始化适配器
     */
    private void initAdapter() {
        dataList = new ArrayList<>();
        adapter = new GrowthIntegralAdapter(dataList);
        adapter.addHeaderView(getHeaderView());
        rlvGrowth.setLayoutManager(new LinearLayoutManager(mActivity));
        rlvGrowth.addItemDecoration(new SuperDividerItemDecoration(new SuperDividerItemDecoration.Builder(context)
                .setDividerColor(0xFFeeeeee)
                .setOrientation(RecyclerView.VERTICAL)
                .setIsShowLastDivide(true)));
        rlvGrowth.setAdapter(adapter);
        adapter.setOnItemClickListener((adapter, view, position) -> {
            GrowthIntegralEntity.Menu  menu = (GrowthIntegralEntity.Menu) adapter.getData().get(position);
            if(menu.getStatus()){
                setResult(ARouterConstant.GROWTH_JUMP_SUCCESS, new Intent().putExtra(ARouterConstant.GROWTH_JUMP, menu.getType()));
                finish();
            }
        });
    }

    /**
     * 初始化recycleview 头部view
     */
    private View getHeaderView() {
        LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.activity_growth_integral_top, null);
        jfCount = view.findViewById(R.id.tv_jf_count);
        jfToday = view.findViewById(R.id.tv_jf_today);
        TextView jfInfo = view.findViewById(R.id.tv_jf_info);
        RelativeLayout jfTop = view.findViewById(R.id.rl_jf_top);
        RelativeLayout jfDetail = view.findViewById(R.id.rl_jf_detail);
        //积分说明
        jfInfo.setOnClickListener(v -> {
            withValueActivity(ARouterConstant.InfoWebviewActivity)
                    .withString(ARouterConstant.TOPBAR_TITLE,"积分说明")
                    .withInt(ARouterConstant.TYPE, ARouterConstant.JIFEN)
                    .navigation();
        });
        //积分排行榜
        jfTop.setOnClickListener(v -> {
            gotoActivity(ARouterConstant.IntegralRankingListActivity);
        });
        //积分贡献
        jfDetail.setOnClickListener(v -> {
            gotoActivity(ARouterConstant.IntegralDetailListActivity);
        });
        view.setLayoutParams(new DrawerLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        return view;
    }


    public void getData(GrowthIntegralEntity entity) {
        this.entity = entity;
        if(entity == null) {
            return;
        }
        this.dataList = entity.getMenu();
        notifyData();
    }

    private void notifyData() {
        jfCount.setText(entity.getAll_credit());
        //今日积分
        String price = "<font color=#727272>今日已贡献 </font><font color=#e1b65c> " + entity.getToday_credit() + "积分</font>";
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            jfToday.setText(Html.fromHtml(price, Html.FROM_HTML_MODE_COMPACT));
        } else {
            jfToday.setText(Html.fromHtml(price));
        }
        if (dataList == null) {
            return;
        }
        adapter.setNewData(dataList);
    }


    @Override
    protected void onResume() {
        super.onResume();
        getP().getData();
    }


}
